package com.thrd.taiexhibition.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.thrd.taiexhibition.R;
import com.thrd.taiexhibition.bean.BeanCity;
import com.thrd.taiexhibition.bean.BeanCityLetter;

import java.util.List;

/**
 * @author: ${唐杰}
 * @date: 2021/3/24
 */
public class HomeCityAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<BeanCityLetter> mBeanCityLetterList;
    public HomeCityAdapter(Context mContext, List<BeanCityLetter> mBeanCityLetterList){
        this.mContext=mContext;
        this.mBeanCityLetterList=mBeanCityLetterList;
    }

    //返回一级列表的个数
    @Override
    public int getGroupCount() {
        return mBeanCityLetterList!=null?mBeanCityLetterList.size():0;
    }
    //返回每个二级列表的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return mBeanCityLetterList.get(groupPosition).getCityList().size();
    }
    //返回一级列表的单个item（返回的是对象）
    @Override
    public Object getGroup(int groupPosition) {
        return mBeanCityLetterList.get(groupPosition);
    }
    //返回二级列表中的单个item（返回的是对象）
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mBeanCityLetterList.get(groupPosition).getCityList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    //每个item的id是否是固定？一般为true
    @Override
    public boolean hasStableIds() {
        return true;
    }
    //【重要】填充一级列表
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        BeanCityLetter headerInfo = (BeanCityLetter) getGroup(groupPosition);
        ViewHolder holder = null;
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.item_city_title, null);
            holder = new ViewHolder();
            holder.mGroupName = (TextView) convertView.findViewById(R.id.tv_title);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mGroupName.setText(TextUtils.isEmpty(headerInfo.getLetter())?"":headerInfo.getLetter());
        return convertView;
    }
    //【重要】填充二级列表
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final BeanCity headerInfo = (BeanCity) getChild(groupPosition,childPosition);
        ViewHolder holder = null;
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.item_city_content, null);
            holder = new ViewHolder();
            holder.mGroupName = (TextView) convertView.findViewById(R.id.tv_content);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mGroupName.setText(TextUtils.isEmpty(headerInfo.getCityName())?"":headerInfo.getCityName());
        holder.mGroupName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClick!=null){
                    onItemClick.onItemClick(headerInfo);
                }
            }
        });
        return convertView;
    }
    //二级列表中的item是否能够被选中？可以改为true
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public void setData(List<BeanCityLetter> listBeanCityLetters) {
        this.mBeanCityLetterList=listBeanCityLetters;
        notifyDataSetChanged();
    }


    private class ViewHolder {
        private TextView mGroupName;
    }

    public OnItemClick getOnItemClick() {
        return onItemClick;
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    private OnItemClick onItemClick;

    public interface OnItemClick{
        void onItemClick(BeanCity headerInfo);
    }
}
